package com.erp.module.JiJianCaiJi

import android.content.Context
import com.erp.bean.JiJianCaiJiBean
import com.erp.Interface.OnUserDataInterface
import com.erp.pull.BasePullP
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2019/3/19/019.
 * 作者：刘洋
 */
class IJiJianCaiJiP(val V : JiJianCaiJiContract.JiJianCaiJiView, val context : Context, var list : MutableList<JiJianCaiJiBean>) : BasePullP(), JiJianCaiJiContract.JiJianCaiJiP {
    var md : JiJianCaiJiContract.JiJianCaiJiMP= JiJianCaiJiModel()
    var name = ""
    init {
        PullV=V
    }
    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        md.getJiJianCaiJiLieBiao(context,page,pagesize,name,onUserDataInterface)
    }

    override fun jsondata(data: String?) {
        if (data==null){
            return
        }
        try {
            val Js= JSONArray(data)
            for(i in 0 until Js.length()){
                val jo=Js.get(i) as JSONObject
                var jiJianCaiJi = JiJianCaiJiBean()
                jiJianCaiJi.id= JsonUtil.getvolue(jo,"id")
                jiJianCaiJi.mcode= JsonUtil.getvolue(jo,"mcode")
                jiJianCaiJi.oid= JsonUtil.getvolue(jo,"oid")
                jiJianCaiJi.creator= JsonUtil.getvolue(jo,"creator")
                jiJianCaiJi.ctime= JsonUtil.getvolue(jo,"ctime")
                jiJianCaiJi.mb_batch= JsonUtil.getvolue(jo,"mb_batch")
                jiJianCaiJi.creator_name= JsonUtil.getvolue(jo,"creator_name")
                jiJianCaiJi.gangwei_name= JsonUtil.getvolue(jo,"gangwei_name")
                list.add(jiJianCaiJi)
            }
        }catch (e : JSONException){
            e.printStackTrace()
        }
    }

    override fun listClean() {
        list.clear()
    }

    override fun putJiJianShuJu(mcode: String) {
        md.setJiJianCaiJi(context,mcode,object : OnUserDataInterface{
            override fun onUserData(data: String?) = V.setJiJianCaiJiOk()

            override fun onNot(throwable: String?) {
                if (throwable != null) {
                    V.setJiJianCaiJiNo(getthrowable(throwable))
                }
            }

        })
    }

    override fun deleteJiJianShuJu(id: String, item: Int) {
        Httputil.deleteJiJianCaiJi(context,id,object : OnUserDataInterface{
            override fun onUserData(data: String?) = V.deleteJiJianCaiJiOk(item)

            override fun onNot(throwable: String?) {
                if (throwable != null) {
                    val reg = "\""
                    val ss = throwable.split(reg.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                    if(ss.size>1) {
                        V.setJiJianCaiJiNo(Myutil.decodeUnicode(ss[1]))
                    }else{
                        V.setJiJianCaiJiNo(getthrowable(throwable))
                    }
                }
            }

        })
    }

    override fun sousuo(name: String) {
        this.name=name
        xiala()
    }

    fun getthrowable(throwable : String) : String{
        val reg = "result:"
        val ss = throwable.split(reg.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        if(ss.size>1) {
            return Myutil.decodeUnicode(ss[1])
        }else{
            return Myutil.decodeUnicode(throwable)
        }
    }
}